﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WHM3.Account
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) { return; }

            RegisterHyperLink.NavigateUrl = "Register.aspx";
            OpenAuthLogin.ReturnUrl = Request.QueryString["ReturnUrl"];

            var returnUrl = HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
            if (!String.IsNullOrEmpty(returnUrl))
            {
                RegisterHyperLink.NavigateUrl += "?ReturnUrl=" + returnUrl;
            }

            if (Session["errCode"] != null)
            {
                this.LoginFailLable.Text = (string)Session["errCode"];
                LoginFailLable.Visible = true;
                Session["errCode"] = null;

            }

            //this means to logout
            Session.Clear();


        }

        protected void Unnamed6_Click(object sender, EventArgs e)
        {
            BL.init();

            TextBox uName = (TextBox)Login1.FindControl("UserName");
            TextBox pwd = (TextBox)Login1.FindControl("Password");
  

            //if admin re-dir to admin
            Employee employee=null;
            try
            {
                employee = BL.getEmployeeById(uName.Text);
            }
            catch
            {
                //there was an error connecting to the DB
                this.LoginFailLable.Text = "System Seems quite busy now.. please try again later";
                LoginFailLable.Visible = true;

                //refresh page with error code
                Session["errCode"] = "System Seems quite busy now.. please try again later";
                Response.Redirect("~/Account/Login.aspx");

            }

            if (employee != null && employee.isAdmin.Equals("True") && employee.Password.Equals(pwd.Text))
            {
                Session["myobject"] = new LoginDetails(employee.id, true, true);
                Response.Redirect("~/AdminPage/admin.aspx");
            }
            else if (employee != null && employee.isManager.Equals("True") && employee.Password.Equals(pwd.Text))
            {
                Session["managerDetails"] = new LoginDetails(employee.id, false, true);
                Response.Redirect("~/ManagerPage/ManagerPage.aspx");
            }
            else if (employee != null && employee.Password.Equals(pwd.Text))
            {
                Session["myobject"] = new LoginDetails(employee.id, false, false);
                Response.Redirect("~/EmployeePage/EmployeePage.aspx");
            }
            else
            {
                //emp==null if username/pass incorrect
                this.LoginFailLable.Text = "Username/Password incorrect";
                LoginFailLable.Visible = true;

                //refresh page with error code
                Session["errCode"] = "Username/Password incorrect";
                Response.Redirect("~/Account/Login.aspx");
            }


        }
    }
}